package com.enjoy.studyfragment.fragment;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.View;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.TieBean;
import com.dou361.dialogui.listener.DialogUIItemListener;
import com.enjoy.mjbaseframe.base.BaseMainFragment;
import com.enjoy.mjbaseframe.util.PermissionUtil;
import com.enjoy.studyfragment.R;
import com.enjoy.studyfragment.activity.TestActivity;
import com.enjoy.studyfragment.adapter.HomeAdapter;
import com.enjoy.studyfragment.bean.HomeBean;
import com.enjoy.studyfragment.util.PictureDialogUtil;
import com.joker.api.wrapper.ListenerWrapper;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.orhanobut.logger.Logger;
import com.yanzhenjie.recyclerview.swipe.SwipeItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

import static com.enjoy.studyfragment.constant.Constants.REQUEST_CAMERA;

/**
 * Created by Administrator on 2018/3/14.
 */

public class HomeFragment extends BaseMainFragment {
    @BindView(R.id.swipeRecycler)
    SwipeMenuRecyclerView swipeRecycler;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    private HomeAdapter adapter;
    private List<HomeBean> mDataList = new ArrayList<>();//底部菜单集合
    private List<LocalMedia> selectList = new ArrayList<>();//拍照或相册选择图片集合

    @Override
    public Object setLayout() {
        return R.layout.fragemnt_home;
    }

    @Override
    public void init() {
        super.init();
        initRecycler();
    }

    private void initRecycler() {
        Logger.d("initRecycler");
        mDataList.add(new HomeBean(0,"弹框效果"));
        mDataList.add(new HomeBean(1,"相册拍照效"));
        mDataList.add(new HomeBean(2,"朋友圈效果"));
        mDataList.add(new HomeBean(3,"QQ分组效果"));
        mDataList.add(new HomeBean(4,"下拉刷新上拉就在更多"));
        mDataList.add(new HomeBean(5,"loading效果"));
        mDataList.add(new HomeBean(6,"webview功能"));
        mDataList.add(new HomeBean(7,"通讯录效果"));
        mDataList.add(new HomeBean(8,"二维码扫码"));
        mDataList.add(new HomeBean(9,"折叠文本"));

        adapter = new HomeAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        swipeRecycler.setLayoutManager(linearLayoutManager);
        swipeRecycler.setSwipeItemClickListener(mItemClickListener); // RecyclerView Item点击监听。
        swipeRecycler.setAdapter(adapter);
        adapter.setNewData(mDataList);
        swipeRecycler.loadMoreFinish(true, true);
        swipeLayout.setEnabled(false);
    }

    /**
     * Item点击监听。
     */
    private SwipeItemClickListener mItemClickListener = new SwipeItemClickListener() {
        @Override
        public void onItemClick(View itemView, int position) {
            switch (position){
                case 0:
                    List<TieBean> strings = new ArrayList<TieBean>();
                    strings.add(new TieBean("拍照"));
                    strings.add(new TieBean("相册"));
                    DialogUIUtils.showSheet(mActivity, strings, "取消", Gravity.BOTTOM, true, true, new DialogUIItemListener() {
                        @Override
                        public void onItemClick(CharSequence text, int position) {
                            DialogUIUtils.showToast(text + "---" + position);
                        }

                        @Override
                        public void onBottomBtnClick() {
                            Toasty.normal(getContext(),"取消").show();
                        }
                    }).show();
                    break;
                case 1:
                    PictureDialogUtil.showPicture(getActivity(),HomeFragment.this,selectList);
                    break;
                case 2:
                    startBrotherFragment(new CircleFriendsFragment());
                    break;
                case 3:
                    startBrotherFragment(new QQGroupFragment());
                    break;
                case 4:
                    startBrotherFragment(new SwipeRecyclerFragment());
                    break;
                case 5:
                    break;
                case 6:
                    break;
                case 7:
                    startBrotherFragment(new ContactFragment());
                    break;
                case 8:
                    //相机权限
                    PermissionUtil.requestPermission(getActivity(),REQUEST_CAMERA,new ListenerWrapper.PermissionRequestListener(){

                        @Override
                        public void permissionGranted(int i) {
                            startBrotherFragment(new QRcodeFragment());
//                            getActivity().startActivity(new Intent(getContext(),QRcodeFragment.class));
                        }

                        @Override
                        public void permissionDenied(int i) {

                        }

                        @Override
                        public void permissionRationale(int i) {

                        }
                    }, new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA});

                    break;
                case 9:
                    startBrotherFragment(new ExpandTextFragment());
                    break;
                    default:break;
            }
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) { //拍照或相册选择返回事件
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择
                    selectList = PictureSelector.obtainMultipleResult(data);
                    for(LocalMedia media : selectList){
                        Logger.d(media.getCompressPath());
                    }
                    break;
            }
        }
    }
}
